package com.tansun.easycare.rule.deployonline.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tansun.easycare.rule.bakproject.service.IRuleProjectZipService;
import com.tansun.easycare.rule.common.JsonMessage;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.deployonline.entity.RuleEnterpoint;
import com.tansun.easycare.rule.deployonline.service.RuleDbunitDownService;
import com.tansun.easycare.rule.deployonline.service.RuleDeployOnlineService;
import com.tansun.easycare.rule.deployonline.service.RuleEnterpointService;

/**
 * 联机部署中流程相关信息的下载 20200228
 */
@Controller
@RequestMapping(value = "/ruleproj/dbunitDown")
public class RuleDbunitDownController extends RuleBaseController {
    @Autowired
    private IRuleProjectZipService ruleProjectZipService;
    @Autowired
    private RuleDbunitDownService ruleDbunitDownService;
    @Autowired
    private RuleDeployOnlineService  ruleDeployOnlineService;
    @Autowired
    private RuleEnterpointService ruleEnterpointService;

    /**
     * 下载联机部署中对应的规则流相关信息 ljt 20200228
     * @param req
     * @param res
     */
    @RequestMapping("dbunitDownRuleFlow")
    public   void    dbunitDownRuleFlow(HttpServletRequest req, HttpServletResponse res,String deploymentId,String[] enterpointIds) {
        OutputStream os =null;
        String userAgent = req.getHeader("User-Agent");
        if (userAgent == null)
            userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36";
        try {
            res.reset();
            res.setContentType("application/octet-stream; charset=utf-8");
            res.setHeader("Content-Disposition", "attachment; filename=" + this.getCurrProjectCode()+".zip");
            os = res.getOutputStream();
            InputStream is =ruleDbunitDownService.dbunitDownRuleFlow(this.getCurrProjectCode(),deploymentId,enterpointIds);
            byte[] b  = new  byte[1024];
            while ((is.read(b) !=-1)) {
                os.write(b);
            }
            os.flush();
            is.close();
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @RequestMapping("findEnterPoint")
    @ResponseBody
    public JsonMessage findEnterByDeploymentId(String deploymentId) throws Exception{
        JsonMessage jsonMessage = new JsonMessage();
        RuleEnterpoint ruleEnterpoint = new RuleEnterpoint();
        ruleEnterpoint.setDeploymentId(deploymentId);
        ruleEnterpoint.setProjectCode(this.getCurrProjectCode());
        List<RuleEnterpoint> enterPoints = this.ruleEnterpointService.findList(ruleEnterpoint);
        jsonMessage.setMessage("true");
        jsonMessage.setData((Serializable)enterPoints);
        return  jsonMessage;
    }

}
